{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a9d513b9",
   "metadata": {},
   "source": [
    "# 字典\n",
    "字典用于在键值对中存储数据值。\n",
    "\n",
    "字典是有序*、可变且不允许重复的集合。\n",
    "\n",
    "从 Python 3.7 版开始，字典是有序的。 在 Python 3.6 及更早版本中，字典是无序的。\n",
    "\n",
    "字典是用大括号写的，有键和值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "277e99e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}\n"
     ]
    }
   ],
   "source": [
    "thisdict = {\n",
    "  \"brand\": \"Ford\",\n",
    "  \"model\": \"Mustang\",\n",
    "  \"year\": 1964\n",
    "}\n",
    "print(thisdict)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42ad8288",
   "metadata": {},
   "source": [
    "## 字典项目\n",
    "字典项是有序的、可变的，并且不允许重复。\n",
    "\n",
    "字典项以键值对的形式呈现，可以使用键名进行引用。\n",
    "### 有序还是无序？\n",
    "从 Python 3.7 版开始，字典是有序的。 在 Python 3.6 及更早版本中，字典是无序的。\n",
    "\n",
    "当我们说字典是有序的时，这意味着项目有一个定义的顺序，并且这个顺序不会改变。\n",
    "\n",
    "无序意味着项目没有定义的顺序，您不能使用索引来引用项目。\n",
    "\n",
    "### 可变\n",
    "字典是可变的，这意味着我们可以在字典创建后更改、添加或删除项目。\n",
    "\n",
    "### 不允许重复\n",
    "字典不能有两个具有相同键的项目："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8a1e818a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Ford', 'model': 'Mustang', 'year': 2020}\n"
     ]
    }
   ],
   "source": [
    "thisdict = {\n",
    "  \"brand\": \"Ford\",\n",
    "  \"model\": \"Mustang\",\n",
    "  \"year\": 1964,\n",
    "  \"year\": 2020\n",
    "}\n",
    "print(thisdict)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7af886bf",
   "metadata": {},
   "source": [
    "## 字典长度\n",
    "要确定字典有多少项，请使用 len() 函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7c8a5b23",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "print(len(thisdict))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df2753d5",
   "metadata": {},
   "source": [
    "## 字典项 - 数据类型\n",
    "字典项中的值可以是任何数据类型："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9d121c2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "thisdict = {\n",
    "  \"brand\": \"Ford\",\n",
    "  \"electric\": False,\n",
    "  \"year\": 1964,\n",
    "  \"colors\": [\"red\", \"white\", \"blue\"]\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b793863b",
   "metadata": {},
   "source": [
    "# 访问项目\n",
    "可以通过引用方括号内的键名来访问字典的项目："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9b69cfa4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mustang\n"
     ]
    }
   ],
   "source": [
    "thisdict = {\n",
    "  \"brand\": \"Ford\",\n",
    "  \"model\": \"Mustang\",\n",
    "  \"year\": 1964\n",
    "}\n",
    "x = thisdict[\"model\"]\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "13e3e0e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mustang\n"
     ]
    }
   ],
   "source": [
    "y = thisdict.get(\"model\")\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c97300ef",
   "metadata": {},
   "source": [
    "## 获取Key\n",
    "keys() 方法将返回字典中所有键的列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "34a09883",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['brand', 'model', 'year'])\n"
     ]
    }
   ],
   "source": [
    "x = thisdict.keys()\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39cd1b69",
   "metadata": {},
   "source": [
    "## 获取值\n",
    "values() 方法将返回字典中所有值的列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1e66bc1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_values(['Ford', 'Mustang', 1964])\n"
     ]
    }
   ],
   "source": [
    "x = thisdict.values()\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e982d71a",
   "metadata": {},
   "source": [
    "## 获取项目\n",
    "items() 方法将返回字典中的每个项目，作为列表中的元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5cd581fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])\n"
     ]
    }
   ],
   "source": [
    "x = thisdict.items()\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3530d52d",
   "metadata": {},
   "source": [
    "## 检查key是否存在\n",
    "要确定字典中是否存在指定的键，请使用 in 关键字："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9d2975b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Yes, 'model' is one of the keys in the thisdict dictionary\n"
     ]
    }
   ],
   "source": [
    "thisdict = {\n",
    "  \"brand\": \"Ford\",\n",
    "  \"model\": \"Mustang\",\n",
    "  \"year\": 1964\n",
    "}\n",
    "if \"model\" in thisdict:\n",
    "  print(\"Yes, 'model' is one of the keys in the thisdict dictionary\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78114209",
   "metadata": {},
   "source": [
    "# 更改词典项目\n",
    "## 更改值\n",
    "可以通过引用其键名来更改特定项目的值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "741f3ec1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Ford', 'model': 'Mustang', 'year': 2018}\n"
     ]
    }
   ],
   "source": [
    "thisdict = {\n",
    "  \"brand\": \"Ford\",\n",
    "  \"model\": \"Mustang\",\n",
    "  \"year\": 1964\n",
    "}\n",
    "thisdict[\"year\"] = 2018\n",
    "print(thisdict)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "775324e2",
   "metadata": {},
   "source": [
    "## 更新字典\n",
    "update() 方法将使用给定参数中的项目更新字典。\n",
    "\n",
    "参数必须是字典或具有键值对的可迭代对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "caaa6c9f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Ford', 'model': 'Mustang', 'year': 2020}\n"
     ]
    }
   ],
   "source": [
    "thisdict = {\n",
    "  \"brand\": \"Ford\",\n",
    "  \"model\": \"Mustang\",\n",
    "  \"year\": 1964\n",
    "}\n",
    "thisdict.update({\"year\": 2020})\n",
    "print(thisdict)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04397363",
   "metadata": {},
   "source": [
    "# 添加字典项目\n",
    "## 添加项目\n",
    "通过使用新的索引键并为其分配值来将项目添加到字典中："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "084e2984",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}\n"
     ]
    }
   ],
   "source": [
    "thisdict = {\n",
    "  \"brand\": \"Ford\",\n",
    "  \"model\": \"Mustang\",\n",
    "  \"year\": 1964\n",
    "}\n",
    "thisdict[\"color\"] = \"red\"\n",
    "print(thisdict)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47bcc85f",
   "metadata": {},
   "source": [
    "## 更新字典\n",
    "update() 方法将使用给定参数中的项目更新字典。 如果该项目不存在，则会添加该项目。\n",
    "\n",
    "参数必须是字典或具有键值对的可迭代对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "90a062e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}\n"
     ]
    }
   ],
   "source": [
    "thisdict = {\n",
    "  \"brand\": \"Ford\",\n",
    "  \"model\": \"Mustang\",\n",
    "  \"year\": 1964\n",
    "}\n",
    "thisdict.update({\"color\": \"red\"})\n",
    "print(thisdict)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71a58efb",
   "metadata": {},
   "source": [
    "# 删除字典项\n",
    "有几种方法可以从字典中删除项目："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "4415e658",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Ford', 'year': 1964}\n"
     ]
    }
   ],
   "source": [
    "thisdict = {\n",
    "  \"brand\": \"Ford\",\n",
    "  \"model\": \"Mustang\",\n",
    "  \"year\": 1964\n",
    "}\n",
    "thisdict.pop(\"model\")\n",
    "print(thisdict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "0c59ef74",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Ford', 'model': 'Mustang'}\n"
     ]
    }
   ],
   "source": [
    "thisdict = {\n",
    "  \"brand\": \"Ford\",\n",
    "  \"model\": \"Mustang\",\n",
    "  \"year\": 1964\n",
    "}\n",
    "thisdict.popitem()\n",
    "print(thisdict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "591ff1fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Ford', 'year': 1964}\n"
     ]
    }
   ],
   "source": [
    "thisdict = {\n",
    "  \"brand\": \"Ford\",\n",
    "  \"model\": \"Mustang\",\n",
    "  \"year\": 1964\n",
    "}\n",
    "del thisdict[\"model\"]\n",
    "print(thisdict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f9f9decf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "thisdict = {\n",
    "  \"brand\": \"Ford\",\n",
    "  \"model\": \"Mustang\",\n",
    "  \"year\": 1964\n",
    "}\n",
    "thisdict.clear()\n",
    "print(thisdict)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b42908b",
   "metadata": {},
   "source": [
    "# 循环字典\n",
    "可以使用 for 循环遍历字典。\n",
    "\n",
    "循环字典时，返回值是字典的键，但也有返回值的方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "0c0bbd48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "brand\n",
      "model\n",
      "year\n"
     ]
    }
   ],
   "source": [
    "thisdict = {\n",
    "  \"brand\": \"Ford\",\n",
    "  \"model\": \"Mustang\",\n",
    "  \"year\": 1964\n",
    "}\n",
    "for x in thisdict:\n",
    "  print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "d7445e1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "brand\n",
      "model\n",
      "year\n"
     ]
    }
   ],
   "source": [
    "for x in thisdict.keys():\n",
    "  print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "b81c86bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ford\n",
      "Mustang\n",
      "1964\n"
     ]
    }
   ],
   "source": [
    "for x in thisdict:\n",
    "  print(thisdict[x])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "703ef4a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ford\n",
      "Mustang\n",
      "1964\n"
     ]
    }
   ],
   "source": [
    "for x in thisdict.values():\n",
    "  print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "61aa4bb6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "brand Ford\n",
      "model Mustang\n",
      "year 1964\n"
     ]
    }
   ],
   "source": [
    "for x, y in thisdict.items():\n",
    "  print(x, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56dcabfa",
   "metadata": {},
   "source": [
    "# 复制字典\n",
    "不能简单地通过键入 dict2 = dict1 来复制字典，因为： dict2 将只是对 dict1 的引用，并且在 dict1 中所做的更改也将自动在 dict2 中进行。\n",
    "\n",
    "有多种方法可以进行复制，一种方法是使用内置的 Dictionary 方法 copy()。另一种方法是使用内置函数 dict()。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "950f1153",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}\n"
     ]
    }
   ],
   "source": [
    "thisdict = {\n",
    "  \"brand\": \"Ford\",\n",
    "  \"model\": \"Mustang\",\n",
    "  \"year\": 1964\n",
    "}\n",
    "mydict = thisdict.copy()\n",
    "print(mydict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "a5624d80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}\n"
     ]
    }
   ],
   "source": [
    "thisdict = {\n",
    "  \"brand\": \"Ford\",\n",
    "  \"model\": \"Mustang\",\n",
    "  \"year\": 1964\n",
    "}\n",
    "mydict = dict(thisdict)\n",
    "print(mydict)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7ea7f17",
   "metadata": {},
   "source": [
    "# 嵌套字典\n",
    "字典可以包含字典，这称为嵌套字典。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "cef7785d",
   "metadata": {},
   "outputs": [],
   "source": [
    "myfamily = {\n",
    "  \"child1\" : {\n",
    "    \"name\" : \"Emil\",\n",
    "    \"year\" : 2004\n",
    "  },\n",
    "  \"child2\" : {\n",
    "    \"name\" : \"Tobias\",\n",
    "    \"year\" : 2007\n",
    "  },\n",
    "  \"child3\" : {\n",
    "    \"name\" : \"Linus\",\n",
    "    \"year\" : 2011\n",
    "  }\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
